package com.tech.model;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Locale;

import static com.tech.utils.Option.*;

/**
 * Created by zouyao on 2023/06/01
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DocModelField implements Serializable {

    @ExcelProperty("序号")
    private String index = ROOT_ID;

    @ExcelProperty("字段名称")
    private String name =  StrUtil.EMPTY;
    private String className = StrUtil.EMPTY;
    private String fieldName = StrUtil.EMPTY;

    public void setName(String str) {
        this.name = str;

        String tmpName = str;

        if (str.contains("_")){
            tmpName = StrUtil.toCamelCase(str);
        }
        if (str.matches("[A-Z]+")){
            tmpName = str.toLowerCase(Locale.ROOT);
        }

        this.className = StrUtil.upperFirst(tmpName);
        this.fieldName = StrUtil.lowerFirst(tmpName);
    }

    @ExcelProperty("字段类型")
    private String type;

    @ExcelProperty("字段说明")
    private String describe = StrUtil.EMPTY;

    @ExcelProperty("是否必输")
    private String required;

    @ExcelProperty("取值范围")
    private Integer range;

    @ExcelProperty("约束条件")
    private Integer condition;

    @ExcelProperty("正则表达式")
    private Integer reg;

    @ExcelProperty("最大长度")
    private Integer maxLength;

    @ExcelProperty("最小长度")
    private Integer minLength;

    @ExcelProperty("备注")
    private String remake;

    /**
     * 归属对象
     */
    private String parentIndex;
    private String dataFlow;
    private String createTime = DateUtil.now();

    public static Tree<String> ofTree(){
        DocModelField field = new DocModelField();
        Tree<String> treeNode = new Tree<>();
        BeanUtil.beanToMap(field).forEach(treeNode::putExtra);
        return treeNode;
    }

    public static Tree<String> ofTree(String name){
        DocModelField field = new DocModelField();
        field.setName(name);
        Tree<String> treeNode = new Tree<>();
        BeanUtil.beanToMap(field).forEach(treeNode::putExtra);
        return treeNode;
    }
}

